home *** CD-ROM | disk | FTP | other *** search
/ ftp.hitl.washington.edu / ftp.hitl.washington.edu.tar / ftp.hitl.washington.edu / pub / people / tsoper / CT Explorer / Eye.cs < prev    next >
Text File  |  2005-06-09  |  9KB  |  379 lines

  1. using System;
  2. using System.Drawing;
  3. //TAKE THESE OUT
  4. using System.ComponentModel;
  5. using System.Collections;
  6. using System.Windows.Forms;
  7. using CsGL.OpenGL;
  8.  
  9.  
  10. //TO DO: come up with better name that VIEWING_SIDE
  11. public enum VIEWING_SIDE
  12. {
  13.     FRONT,
  14.     BACK,
  15.     LEFT,
  16.     RIGHT,
  17.     TOP,
  18.     BOTTOM
  19. }
  20.  
  21. public class Eye
  22. {
  23.     //Attributes
  24.     private float[] volume = new float[3];        //volume of the model we're viewing
  25.     //TO DO: change to startIndex
  26.     private float[] index = new float[3];        //position to place origin of model
  27.     private float[] orthoVolume = new float[6];    //the volume bounds relative to the eye
  28.     private float[] position = new float[3];    //position of the eye
  29.     private float[] target = new float[3];        //where the eye is looking
  30.     private float[] forwardVector = new float[3]; //the forward vector of the eye
  31.     private float[] rightVector = new float[3];      //the right vector relative to the eye
  32.     
  33.     private float maxZoom = 1.0f;                //max zoom is 1 (non-zoom-in-able) to start
  34.     private float minZoom = 1.0f;                //min zoom is 1 (non-zoom-out-able) to start
  35.     private float[] zoomFocus = new float[2];    //focus point of the zoom
  36.     private float zoomScale = 1.0f;                //zoom scale 
  37.     private float zoomLeft;                        //ortho volume values when zoomed;
  38.     private float zoomRight;
  39.     private float zoomBottom;
  40.     private float zoomTop;
  41.     private float[] zoomCenter = new float[2];    //center position of zoom view
  42.  
  43.     //Properties
  44.     public float[] Volume
  45.     {
  46.         get
  47.         {
  48.             return (float[])volume.Clone();
  49.         }
  50.         set
  51.         {
  52.             volume = (float[])value.Clone();
  53.         }
  54.         
  55.     }
  56.  
  57.     public float[] Index
  58.     {
  59.         get
  60.         {
  61.             return (float[])index.Clone();
  62.         }
  63.         set
  64.         {
  65.             index = (float[])value.Clone();
  66.         }
  67.     }
  68.  
  69.     public float[] Position
  70.     {
  71.         get
  72.         {
  73.             return (float[])position.Clone();
  74.         }
  75.         set
  76.         {
  77.             position = (float[])value.Clone();    
  78.         }
  79.     }
  80.  
  81.     public float[] Target
  82.     {
  83.         get
  84.         {
  85.             return (float[])target.Clone();
  86.         }
  87.         set
  88.         {
  89.             target = (float[])value.Clone();
  90.         }
  91.     }
  92.     
  93.     private float[] upVector = new float[3]; 
  94.     public float[] UpVector
  95.     {
  96.         get
  97.         {
  98.             return (float[])upVector.Clone();
  99.         }
  100.         set
  101.         {
  102.             upVector = (float[])value.Clone();
  103.         }
  104.     }
  105.  
  106.     public float[] RightVector
  107.     {
  108.         get
  109.         {
  110.             return (float[])rightVector.Clone();
  111.         }
  112.     }
  113.     
  114.  
  115.  
  116.     public float Left
  117.     {
  118.         get
  119.         {
  120.             if (zoomScale.Equals(1.0f))
  121.                 return orthoVolume[0];
  122.             else
  123.                 return zoomLeft;
  124.         }
  125.     }
  126.  
  127.     public float Right
  128.     {
  129.         get
  130.         {
  131.             if (zoomScale.Equals(1.0f))
  132.                 return orthoVolume[1];
  133.             else
  134.                 return zoomRight;
  135.  
  136.         }
  137.     }
  138.     public float Bottom
  139.     {
  140.         get
  141.         {
  142.             if(zoomScale.Equals(1.0f))
  143.                 return orthoVolume[2];
  144.             else
  145.                 return zoomBottom;
  146.         }
  147.     }
  148.     public float Top
  149.     {
  150.         get
  151.         {
  152.             if(zoomScale.Equals(1.0f))
  153.                 return orthoVolume[3];
  154.             else
  155.                 return zoomTop;
  156.         }
  157.     }
  158.     public float Near
  159.     {
  160.         get
  161.         {
  162.                 return orthoVolume[4];
  163.         }
  164.     }
  165.     public float Far
  166.     {
  167.         get
  168.         {
  169.                 return orthoVolume[5];
  170.  
  171.         }
  172.     }
  173.     
  174.  
  175.  
  176.  
  177.     public float MaxZoom    //get/set the maxZoom
  178.     {
  179.         get
  180.         {
  181.             return maxZoom;
  182.         }
  183.         set
  184.         {
  185.             if(value > 1) //max zoom must be greater than unity
  186.             {
  187.                 maxZoom = value;
  188.             }
  189.         }
  190.     }
  191.         
  192.     public float MinZoom    //get/set the min zoom
  193.     {
  194.         get
  195.         {
  196.             return minZoom;
  197.         }
  198.         set
  199.         {
  200.             minZoom = value;
  201.         }
  202.     }
  203.     public float ZoomScale    //get zoom scale
  204.     {
  205.         get
  206.         {
  207.             return zoomScale;
  208.         }
  209.     }
  210.  
  211.     //TO DO: make this a 2D position component
  212.     public float[] ZoomFocus    //set the focus of the zoom
  213.     {
  214.         get
  215.         {
  216.             return (float[])zoomFocus.Clone();
  217.         }
  218.         set
  219.         {
  220.             zoomFocus = (float[])value.Clone();
  221.         }
  222.     }
  223.             
  224.  
  225.     private float BufferDistance = 100; //buffer distance from volume
  226.  
  227.     //TAKE THIS OUT
  228.     public Label lbl = new Label();
  229.  
  230.     //constructor
  231.     public Eye()
  232.     {
  233.     }
  234.  
  235.     public void SetViewingSide( VIEWING_SIDE viewingSide )
  236.     {
  237.         switch( viewingSide )
  238.         {
  239.             case VIEWING_SIDE.LEFT:
  240.                 position[0] = index[0] - BufferDistance;
  241.                 position[1] = index[1] + volume[1]/2.0f;
  242.                 position[2] = index[2] + volume[2]/2.0f;
  243.                 orthoVolume[0] = -volume[1]/2;
  244.                 orthoVolume[1] = volume[1]/2;
  245.                 orthoVolume[2] = -volume[2]/2;
  246.                 orthoVolume[3] = volume[2]/2;
  247.                 orthoVolume[4] = 0;
  248.                 orthoVolume[5] = 2*BufferDistance + volume[0];
  249.                 upVector[0] = upVector[1] = 0.0f;
  250.                 upVector[2] = 1.0f;
  251.                 forwardVector.Initialize();
  252.                 forwardVector[0] = 1.0f;
  253.                 target = (float[])position.Clone();
  254.                 target[0] += 1;
  255.                 break;
  256.             case VIEWING_SIDE.RIGHT:
  257.                 position[0] = index[0] + volume[0] + BufferDistance;
  258.                 position[1] = index[1] + volume[1]/2.0f;
  259.                 position[2] = index[2] + volume[2]/2.0f;
  260.                 orthoVolume[0] = -volume[1]/2;
  261.                 orthoVolume[1] = volume[1]/2;
  262.                 orthoVolume[2] = -volume[2]/2;
  263.                 orthoVolume[3] = volume[2]/2;
  264.                 orthoVolume[4] = 0;
  265.                 orthoVolume[5] = 2*BufferDistance + volume[0];
  266.                 upVector[0] = upVector[1] = 0.0f;
  267.                 upVector[2] = 1.0f;
  268.                 forwardVector.Initialize();
  269.                 forwardVector[0] = -1;
  270.                 target = (float[])position.Clone();
  271.                 target[0] -= 1;
  272.                 break;
  273.             case VIEWING_SIDE.TOP:
  274.                 position[0] = index[0] + volume[0]/2.0f;
  275.                 position[1] = index[1] + volume[1]/2.0f;
  276.                 position[2] = index[2] + volume[2] + BufferDistance;
  277.                 orthoVolume[0] = -volume[0]/2;
  278.                 orthoVolume[1] = volume[0]/2;
  279.                 orthoVolume[2] = -volume[1]/2;
  280.                 orthoVolume[3] = volume[1]/2;
  281.                 orthoVolume[4] = 0;
  282.                 orthoVolume[5] = 2*BufferDistance + volume[2];
  283.                 upVector[0] = upVector[2] = 0.0f;
  284.                 upVector[1] = 1.0f;
  285.                 forwardVector.Initialize();
  286.                 forwardVector[2] = -1;
  287.                 target = (float[])position.Clone();
  288.                 target[2] -= 1;
  289.                 break;
  290.             case VIEWING_SIDE.BOTTOM:
  291.                 position[0] = index[0] + volume[0]/2.0f;
  292.                 position[1] = index[1] + volume[1]/2.0f;
  293.                 position[2] = index[2] - BufferDistance;
  294.                 orthoVolume[0] = -volume[0]/2;
  295.                 orthoVolume[1] = volume[0]/2;
  296.                 orthoVolume[2] = -volume[1]/2;
  297.                 orthoVolume[3] = volume[1]/2;
  298.                 orthoVolume[4] = 0;
  299.                 orthoVolume[5] = 2*BufferDistance + volume[2];
  300.                 upVector[0] = upVector[2] = 0.0f;
  301.                 upVector[1] = 1.0f;
  302.                 forwardVector.Initialize();
  303.                 forwardVector[2] = 1;
  304.                 target = (float[])position.Clone();
  305.                 target[2] += 1;
  306.                 break;
  307.             case VIEWING_SIDE.FRONT:
  308.                 position[0] = index[0] + volume[0]/2.0f;
  309.                 position[1] = index[1] - BufferDistance;
  310.                 position[2] = index[2] + volume[2]/2.0f;
  311.                 orthoVolume[0] = -volume[0]/2;
  312.                 orthoVolume[1] = volume[0]/2;
  313.                 orthoVolume[2] = -volume[2]/2;
  314.                 orthoVolume[3] = volume[2]/2;
  315.                 orthoVolume[4] = 0;
  316.                 orthoVolume[5] = 2*BufferDistance + volume[1];
  317.                 upVector[0] = upVector[1] = 0.0f;
  318.                 upVector[2] = 1.0f;
  319.                 forwardVector.Initialize();
  320.                 forwardVector[1] = 1;
  321.                 target = (float[])position.Clone();
  322.                 target[1] += 1;
  323.                 break;
  324.             default:
  325.                 break;
  326.  
  327.         }
  328.         rightVector[0] = forwardVector[1]*upVector[2] - 
  329.             forwardVector[2]*upVector[1];
  330.         rightVector[1] = forwardVector[2]*upVector[0] - 
  331.             forwardVector[0]*upVector[2];
  332.         rightVector[2] = forwardVector[0]*upVector[1] - 
  333.             forwardVector[1]*upVector[0];
  334.     }
  335.  
  336.     //zooms in on a subregion
  337.     public void SetZoom(float zs)
  338.     {
  339.         //make sure the zoom is in range
  340.         zoomScale = zs < maxZoom ? zs:maxZoom;
  341.         zoomScale = zs > minZoom ? zs:minZoom;
  342.  
  343.         //find the center of the zoomed view (equal to zoomFocus at maxZoom)
  344.         //using parametric equations with the variable 't'    
  345.         float t = (zoomScale - 1)/zoomScale;
  346.  
  347.         //TO DO: zoom out for when zoom is < 1 and > 0
  348.         
  349.  
  350.         //because center is assumed to be 0,0, parametric equations are simplified
  351.         //from zoomCenter = center(1-t) + zoomFocus*t
  352.         if(t < 1 && t > 0)
  353.         {
  354.             zoomCenter[0] = zoomFocus[0]*t;
  355.             zoomCenter[1] = zoomFocus[1]*t;
  356.         }
  357.         else
  358.             zoomCenter[0] = zoomCenter[1] = 0; //when zooming out, always focus on center
  359.  
  360.         zoomLeft = zoomCenter[0] + orthoVolume[0]/zoomScale;
  361.         zoomRight = zoomCenter[0] + orthoVolume[1]/zoomScale;
  362.         zoomBottom = zoomCenter[1] + orthoVolume[2]/zoomScale;
  363.         zoomTop = zoomCenter[1] + orthoVolume[3]/zoomScale;
  364.         this.lbl.Text = zoomFocus[0] + "," + zoomFocus[1]+ ","+t;
  365.             Application.DoEvents();
  366.     }
  367.  
  368.     public void SetView()
  369.     {
  370.         GL.glEnable(GL.GL_DEPTH_TEST);
  371.         GL.glMatrixMode(GL.GL_MODELVIEW);
  372.         GL.glLoadIdentity();
  373.         GL.glOrtho(this.Left,this.Right,this.Bottom,this.Top,
  374.             this.Near,this.Far);
  375.         GL.gluLookAt(this.Position[0],this.Position[1],this.Position[2],
  376.             this.Target[0],this.Target[1],this.Target[2],
  377.             this.UpVector[0],this.UpVector[1],this.UpVector[2]);
  378.     }
  379. }